package com.changgou.seckill.service;

import com.changgou.seckill.pojo.SeckillGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<SeckillGoods> list(String time) {
        //1. 根据时间菜单到redis中获取秒杀商品列表数据
        List<SeckillGoods> seckillGoodsList = redisTemplate.boundHashOps(time).values();

        //2. 遍历秒杀商品列表数据
        if (seckillGoodsList != null) {
            for (SeckillGoods seckillGoods : seckillGoodsList) {

                //3. 将最新的商品剩余库存数据, 放入秒杀商品对象中
                String stockCountStr = String.valueOf(redisTemplate.opsForValue().get(String.valueOf(seckillGoods.getId())));
                int stockCount = Integer.parseInt(stockCountStr);
                seckillGoods.setStockCount(stockCount);
            }
        }

        return seckillGoodsList;
    }
}
